package com.swak.netty.echo;

import com.swak.utils.Fmt;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

public class Handler extends ChannelDuplexHandler {

	@Override
	public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
		Fmt.out.println("Handler write before");
		super.write(ctx, msg, promise);
		Fmt.out.println("Handler write after");
		promise.addListener(future -> {
			Fmt.out.println("Handler write finish :" + Thread.currentThread().getName());
		});
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		Fmt.out.println("Handler channelRead");
		super.channelRead(ctx, msg);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		Fmt.out.println("数据异常： " + cause);
	}
}
